<?php
namespace App\Http\Controllers;
use App\Lead;
use App\AdminUser;
use App\Customer;
use App\Contact;
use Psy\Util\Json;
use Illuminate\Support\Facades\DB;

class LeadController extends Controller{
    /**
     * 展示通讯录
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function setting(Contact $contact){

        return view('admin.test.contact',compact('contact'));
    }
    public function index()
    {
        return view('admin.leads.index');
    }
    /**
     * 仍回线索池
     */
    public function lead_roll(){
        $sql="UPDATE contacts SET is_lead=0 where";
        $column="";
        for($i=0;$i<count($_REQUEST);$i++)
        {
            if($i==(count($_REQUEST)-1)){
                $column=$column."id=".$_REQUEST[$i];
            }else{
                $column=$column."id=".$_REQUEST[$i]." "."or"." ";
            }
        }
        $sql=$sql." ".$column;
        //return $sql;die;
        if(DB::select($sql)){
            return -1;
        }
        else {
            return 1;
        }
    }
    //线索批量重分配页面
    public function translation(){
        $users=AdminUser::get();
        return view('admin.customer.translation',compact('users'));
    }
    /**
     * 线索批量重分配
     * @param $id 线索ID
     * @param $uid  用户ID
     */
    public function lead_Redistribution(){
        //dd($_REQUEST);
        $sql="UPDATE contacts SET is_lead=1,user_id=".$_REQUEST['user_id']." where";
        $column="";
        for($i=0;$i<count($_REQUEST['lists']);$i++)
        {
            if($i==(count($_REQUEST['lists'])-1)){
                $column=$column."id=".$_REQUEST['lists'][$i];
            }else{
                $column=$column."id=".$_REQUEST['lists'][$i]." "."or"." ";
            }
        }
        $sql=$sql." ".$column;

        //return $sql;die;
        if (DB::update($sql)>0){
            return  '<script type="text/javascript">window.parent.location.reload()</script>';
        }

        //Contact::where('id', $_REQUEST['id'])->update(['user_id'=>$uid]);
    }
    //线索转换客户界面
    public function lead_to_customer($id){
        //获取联系人数据
        $contacts=Contact::where('id',$id)->first();
        //获取处理人数据
        $users=AdminUser::get();
        return view('admin.customer.lead_to_customer',compact('users','contacts'));
    }
    //线索到客户实现逻辑
    public function to_customer($id){

        //验证
        //将是否客户变为1 这样客户表就与联系表关联起来了

        //


        $prov=explode("_", $_REQUEST['prov'])[3];//省
        $city=explode("_", $_REQUEST['city'])[3];//市
        $dist=$_REQUEST['dist'];//区
        //$diz=$_REQUEST['diz'];//镇村
        $address=$prov.'-'.$city.'-'.$dist;//地址
        $param['customer_level']=$_REQUEST['customer_level'];
        $param['customer_type']=$_REQUEST['customer_type'];
        //$param['customer_status']=$_REQUEST['customer_status'];
        $param['company_size']=$_REQUEST['customer_size'];
        $param['company_address']=$address;
        $param['contact_cycle']=$_REQUEST['contact_cycle'];
        $param['next_contact']=$_REQUEST['next_contact'];
        $param['user_id']=$_REQUEST['user_id'];
        $param['contact_id']=1;
        $param['finance_id']=1;

        Contact::where('id', $id)->update(['is_customer'=>1]);
        Customer::create($param);
        //添加完毕返回
        //$users=AdminUser::get();
        //view('admin.customer.index');
        $arr=[
            'code'=>200
        ];

        return $arr;
    }
    //批量删除
    public function del(){
        $sql="delete from contacts where";
        $column="";
        for($i=0;$i<count($_REQUEST);$i++)
        {
            if($i==(count($_REQUEST)-1)){
                $column=$column."id=".$_REQUEST[$i];
            }else{
                $column=$column."id=".$_REQUEST[$i]." "."or"." ";
            }
        }
        $sql=$sql." ".$column;
        DB::select($sql);
        return back();
        // $this->DeleteAll( $_REQUEST); //另类删除手法
    }
    /**
     * 数据接口
     */
    public function data()
    {
        $count=Lead::count();
        if ($count>0) {
            if(isset($_GET['key']) && $_GET['key']['id']!="") {
                $key= $_GET['key']['id'];//搜索序列号
                $users = Lead::where(['id' => $key])->get();
            }
            else{
                $page = $_GET['page']; //当前页数
                $limit = $_GET['limit']; //每页的数量
                $page_count = ($page - 1) * $limit;
                $users = Lead::orderBy('created_at', 'desc')->offset($page_count)->limit($limit)->with('type')->get();
                dd(json_encode($users[0]['type']));
                echo $users[0]['type'][0]['name'];die;
                $users[0]['user_id']=$users[0]['type']['name'];
            }
            return json_encode(
                [
                    "code" => 0,
                    'msg' => '',
                    'count' => $count,
                    'data' => $users
                ]
            );
        } else{
            return [
                'code'=>0,
                "msg"=>"",
                "count"=>0,
                "data"=>[]
            ];
        }
    }
    /**
     * 添加界面
     */
    public function create(){
        $users=AdminUser::get();
        return view('admin.leads.create',compact('users'));
    }

    /**
     * 添加操作逻辑简单实现
     */
    public function store(){
        $data=$_REQUEST;
        //dd($data);
        $contact=array();
        $prov=$data['prov'];//省
        $city=$data['city'];//市
        $dist=$data['dist'];//区
        $contact['source']=$data['source'];
        $contact['name']=$data['name'];
        $contact['company_name']=$data['company_name'];
        $contact['position']=$data['position'];
        if($data['gender']=='先生')
        {
            $contact['gender']='男';
        }else{
            $contact['gender']='女';
        }
        $address=$prov.$city.$dist;//地址
        $contact['address']=$address;
        $contact['phone']=$data['phone'];
        $contact['email']=$data['email'];
        //$contact['remark']=$data['remark'];
        $contact['next_time']=$data['next_time'];
        //$contact['is_lead'] =1;//是否有人负责
        //$contact['is_customer'] =1; //是否为客户
        $contact['user_id']=$data['user_id'];
        Contact::create($contact);
        //Lead::create($param);
        return redirect("/admin/customer");
    }
    /*
     * 删除
     */
    public function delete($id)
    {
        Lead::where('id', '=', $id)->delete();
        return back();
    }
    public function store_edit(Contact $contact){
        $data=$_REQUEST;
        $prov=$data['prov'];//省
        $city=$data['city'];//市
        $dist=$data['dist'];//区
        $contact->source=$data['source'];
        $contact->name=$data['name'];
        $contact->company_name=$data['company_name'];
        $contact->position=$data['position'];
        if($data['gender']=='男')
        {
            $contact->gender='男';
        }else{
            $contact->gender='女';
        }
        $address=$prov.$city.$dist;//地址
        $contact->address=$address;
        $contact->phone=$data['phone'];
        $contact->email=$data['email'];
        //$contact['remark']=$data['remark'];
        $contact->next_time=$data['next_time'];
        //$contact['is_lead'] =1;//是否有人负责
        //$contact['is_customer'] =1; //是否为客户
        $contact->user_id=$data['user_id'];
        $contact->save();
        return  '<script type="text/javascript">window.parent.location.reload()</script>';
    }
    //修改界面
    public function edit(Contact $contact){
        $users=AdminUser::get();
        return view('admin.leads.update',compact('contact','users'));
    }
    //修改逻辑
    public function update(){
      $filds=$this->array_hebing($_REQUEST,'name','company_name','industry','position','address','phone');//传入需要更新的字段
      if(empty($filds))  //判断数据是否为空 是则不修改
      {
          return ['statu'=>'0'];
      }
      else{
          Contact::where('id', $_REQUEST['id'])->update($filds);
          return ['statu'=>'200'];
      }

    }
    //批量删除逻辑
    //原理则是前端发送批量删除的ID组成的数组 通过后端逻辑进行处理

    /**
     * //组成一个我所要的数组用于数据库更新 并且
     * @param array ...$numbers 传进去的字段
     * @return array /￥/返回一个键=字段 值=数值的数组
     */
    public function array_hebing($Data,...$numbers){
      //  print_r($Data);die;

        $data=Contact::where('id', $Data['id'])->first();
        //print_r($_REQUEST);die;
        $filds=[];
        if($Data && is_array($Data)){   //组成一个我所要的数组用于数据库更新
            //print_r($_REQUEST);die;
            foreach($numbers as $fild){
                foreach ($Data as $key=> $value){
                    if($fild==$key)
                    {
                            $filds[$fild] = $value;
                            continue;
                    }
                }
            }
            $start=0;  //循环记数
            foreach($filds as $key=>$value)  //删除请求更新字段中没有修改的字段
            {
                if($data[$key]==$filds[$key]) {   //判断是否相同 相同则从需要更新的数组中删除该键值
                        array_splice($filds, $start, 1);
                        $start=$start-1;
                }
                $start=$start+1;
            }
        }
        return $filds;
    }
}